import { Module } from 'vuex';

interface DeviceStore {
  openedDeviceId: string[];
}

export const deviceStore: Module<DeviceStore, any> = {
  namespaced: true,
  state: {
    openedDeviceId: []
  },

  actions: {
    addOpenedDevice({ state }, deviceId: string) {
      if (state.openedDeviceId.includes(deviceId)) {
        return;
      }
      state.openedDeviceId = [...state.openedDeviceId, deviceId];
    },

    removeOpenedDevice({state}, deviceId: string) {
      const index = state.openedDeviceId.findIndex(id => id === deviceId);
      if (index !== -1) {
        const deviceIds = [...state.openedDeviceId];
        deviceIds.splice(index, 1);
        state.openedDeviceId = deviceIds;
      }
    }
  }
};
